﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

public struct SavedGame
{
    public string Name;
    public int Charisma;
    public int Intelligence;
    public int Perception;
    public Day MaxDay;
}

class LoadGameScreen : MenuScreen
{
    int selected = 0;
    Texture2D back;
    SpriteFont font;
    List<SavedGame> savedGames;

    public LoadGameScreen() : base()
    {
        SavedGameManager save = new SavedGameManager();
        savedGames = save.GetSavedGames();

        TransitionOnTime = TimeSpan.Zero;
        TransitionOffTime = TimeSpan.Zero;
    }

    public override void LoadContent()
    {
        base.LoadContent();

        font = ScreenManager.Game.Content.Load<SpriteFont>("SpriteFont2");

        back = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/back2");
    }

    public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
    {
        base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
    }

    public override void HandleInput()
    {
        //base.HandleInput();

        if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
        {
            selected++;
            if (selected == savedGames.Count)
                selected = savedGames.Count - 1;
        }
        if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
        {
            selected--;
            if (selected < 0)
                selected = 0;
        }
        if (InputManager.IsActionTriggered(InputManager.Action.Ok))
        {
            if (savedGames.Count == 0)
                return;
            ScreenManager.AddScreen(new DaySelectionScreen(savedGames[selected].MaxDay, savedGames[selected].Name,
                savedGames[selected].Charisma, savedGames[selected].Intelligence, savedGames[selected].Perception));
            ExitScreen();
        }
        if (InputManager.IsActionTriggered(InputManager.Action.Back))
        {
            ExitScreen();
        }
    }

    public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
    {
        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();

        spriteBatch.Draw(back, Vector2.Zero, Color.White);

        if (savedGames.Count == 0)
        {
            string aux = "Você não tem personagens salvos.";
            spriteBatch.DrawString(font, aux, new Vector2(Globals.ScreenWidth / 2 - font.MeasureString(aux).X / 2, 
                Globals.ScreenHeight / 2 - font.MeasureString(aux).Y / 2), Color.White);

            spriteBatch.End();
            return;
        }

        spriteBatch.DrawString(font, savedGames[selected].Name, new Vector2(100, 50), Color.White);
        spriteBatch.DrawString(font, "Carisma: " + savedGames[selected].Charisma, new Vector2(150, 120), Color.White);
        spriteBatch.DrawString(font, "Inteligência: " + savedGames[selected].Intelligence, new Vector2(150, 170), Color.White);
        spriteBatch.DrawString(font, "Percepção: " + savedGames[selected].Perception, new Vector2(150, 220), Color.White);
        spriteBatch.DrawString(font, "Dia: " + (int) savedGames[selected].MaxDay, new Vector2(650, 120), Color.White);

        int j=0;

        int auxsele = Math.Min(selected, Math.Max(selected - 2, 0));
        int auxmax = Math.Min(selected + 3, savedGames.Count);

        if (auxsele == selected)
            j = 2; 
        if (auxsele + 1 == selected)
            j = 1;

        for (int i = auxsele; i < auxmax; i++)
        {
            Color color = Color.White;
            if(i == selected)
            {
                color = Color.Red;
            }
            else
                if (i == auxsele || i == auxmax - 1)
                {
                    color.A = 70;
                }
                else
                {
                    color.A = 170;
                }

            spriteBatch.DrawString(font, savedGames[i].Name,
                new Vector2(Globals.ScreenWidth / 2 - font.MeasureString(savedGames[i].Name).X / 2, 300 + j * (font.MeasureString(savedGames[i].Name).Y + 10)), 
                color);

            j++;
        }

        spriteBatch.End();
        base.Draw(gameTime);
    }

}
